<template>
    <el-form :model="form" status-icon :rules="rules" ref="form" label-width="100px"  @submit.native.prevent>
        <el-form-item label="帐号" prop="username">
            <el-input v-model="form.username" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="form.password" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item v-if="showVerify">
            <StSlideVerify ref="slideVerify" @success="()=>{verifyCodeSuccess = true}"></StSlideVerify>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" native-type="submit" @click="submitForm()" :loading="loginLoading">登录</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        name: "StElLogin",
        props: {
            url: String
        },
        created: function () {
            this.loginErrorCount()
        },
        data (){
            return {
                loginLoading: false,
                showVerify: false,
                verifyCodeSuccess: false,
                form:{
                    username: null,
                    password: null
                },
                rules: {
                    username: [
                        {required: true, message: '帐号不能为空'}
                    ],
                    password: [
                        {required: true, message: '密码不能为空'}
                    ]
                }
            }
        },
        methods: {
            loginErrorCount(){
                let errorCount =  this.$stStorageTools.sessionGet('stElLoginErrorCount')
                if(errorCount >= 2){
                    this.verifyCodeSuccess = false
                    this.showVerify = true
                    if(this.$refs.slideVerify){
                        this.$refs.slideVerify.reset()
                    }
                }
            },
            submitForm() {
                let self = this
                if(this.showVerify && !this.verifyCodeSuccess){
                    this.$message.info('请完成验证码操作')
                    return
                }
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        self.loginLoading = true
                        self.axios.post(self.url,
                            `username=${self.form.username}&password=${self.form.password}`).then(res => {
                                this.$stStorageTools.sessionRemove('stElLoginErrorCount')
                            self.$message.success('登录成功')
                            self.$emit('success',res)
                        }).catch(error => {
                            if (error.response) {
                                if(error.response.status == 401){
                                    self.$message.error("用户名或密码不正确")
                                    let errorCount =  this.$stStorageTools.sessionGet('stElLoginErrorCount')
                                    this.$stStorageTools.sessionSet('stElLoginErrorCount',errorCount? ++errorCount : 1)
                                    this.loginErrorCount()
                                    return
                                }
                                if (error.response.data && error.response.data.errorMsg) {
                                    self.$message.error(error.response.data.errorMsg);
                                }else {
                                    self.$message.error('网络错误或服务不可用')
                                }
                            }else{
                                self.$message.error('网络错误或服务不可用')
                            }
                        }).finally(()=>{
                            self.loginLoading = false
                        })
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>

<style scoped>

</style>